﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace QLNHK.BLL
{
	public partial class NhanKhau_QuaTrinh : Ent_Base, ICloneable
	{
        #region Variables
        
		private int _iD;
		private DateTime _ngayCapNhat = new DateTime(1753, 1, 1);
		private int _loaiCapNhat;
		private string _noiDungThayDoi = string.Empty;
		private DateTime _ngayChuyenDen = new DateTime(1753, 1, 1);
		private string _thuongTruTruocChuyenDen = string.Empty;
		private DateTime _ngayChuyenDi = new DateTime(1753, 1, 1);
		private string _noiChuyenDen = string.Empty;
		private DateTime _ngayMat = new DateTime(1753, 1, 1);
		private long _nhanKhauID;
		private int _canBoQuanLyID;
        
        #endregion
    
        //---------------------------------------------------------------------------------------------
        
		#region Properties.
		
		public int ID 
        { 
            get { return  _iD; } 
            set
            {
                _iD = value;
                OnPropertyChanged("ID");
            }
        }
		public DateTime NgayCapNhat 
        { 
            get { return  _ngayCapNhat; } 
            set
            {
                _ngayCapNhat = value;
                OnPropertyChanged("NgayCapNhat");
            }
        }
		public int LoaiCapNhat 
        { 
            get { return  _loaiCapNhat; } 
            set
            {
                _loaiCapNhat = value;
                OnPropertyChanged("LoaiCapNhat");
            }
        }
		public string NoiDungThayDoi 
        { 
            get { return  _noiDungThayDoi; } 
            set
            {
                _noiDungThayDoi = value;
                OnPropertyChanged("NoiDungThayDoi");
            }
        }
		public DateTime NgayChuyenDen 
        { 
            get { return  _ngayChuyenDen; } 
            set
            {
                _ngayChuyenDen = value;
                OnPropertyChanged("NgayChuyenDen");
            }
        }
		public string ThuongTruTruocChuyenDen 
        { 
            get { return  _thuongTruTruocChuyenDen; } 
            set
            {
                _thuongTruTruocChuyenDen = value;
                OnPropertyChanged("ThuongTruTruocChuyenDen");
            }
        }
		public DateTime NgayChuyenDi 
        { 
            get { return  _ngayChuyenDi; } 
            set
            {
                _ngayChuyenDi = value;
                OnPropertyChanged("NgayChuyenDi");
            }
        }
		public string NoiChuyenDen 
        { 
            get { return  _noiChuyenDen; } 
            set
            {
                _noiChuyenDen = value;
                OnPropertyChanged("NoiChuyenDen");
            }
        }
		public DateTime NgayMat 
        { 
            get { return  _ngayMat; } 
            set
            {
                _ngayMat = value;
                OnPropertyChanged("NgayMat");
            }
        }
		public long NhanKhauID 
        { 
            get { return  _nhanKhauID; } 
            set
            {
                _nhanKhauID = value;
                OnPropertyChanged("NhanKhauID");
            }
        }
		public int CanBoQuanLyID 
        { 
            get { return  _canBoQuanLyID; } 
            set
            {
                _canBoQuanLyID = value;
                OnPropertyChanged("CanBoQuanLyID");
            }
        }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Methods
		protected static List<NhanKhau_QuaTrinh> ConvertToCollection(IDataReader reader)
		{
			List<NhanKhau_QuaTrinh> collection = new List<NhanKhau_QuaTrinh>();
			while (reader.Read())
			{
				NhanKhau_QuaTrinh entity = new NhanKhau_QuaTrinh();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayCapNhat"))) entity.NgayCapNhat = reader.GetDateTime(reader.GetOrdinal("NgayCapNhat"));
				if (!reader.IsDBNull(reader.GetOrdinal("LoaiCapNhat"))) entity.LoaiCapNhat = reader.GetInt32(reader.GetOrdinal("LoaiCapNhat"));
				if (!reader.IsDBNull(reader.GetOrdinal("NoiDungThayDoi"))) entity.NoiDungThayDoi = reader.GetString(reader.GetOrdinal("NoiDungThayDoi"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayChuyenDen"))) entity.NgayChuyenDen = reader.GetDateTime(reader.GetOrdinal("NgayChuyenDen"));
				if (!reader.IsDBNull(reader.GetOrdinal("ThuongTruTruocChuyenDen"))) entity.ThuongTruTruocChuyenDen = reader.GetString(reader.GetOrdinal("ThuongTruTruocChuyenDen"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayChuyenDi"))) entity.NgayChuyenDi = reader.GetDateTime(reader.GetOrdinal("NgayChuyenDi"));
				if (!reader.IsDBNull(reader.GetOrdinal("NoiChuyenDen"))) entity.NoiChuyenDen = reader.GetString(reader.GetOrdinal("NoiChuyenDen"));
				if (!reader.IsDBNull(reader.GetOrdinal("NgayMat"))) entity.NgayMat = reader.GetDateTime(reader.GetOrdinal("NgayMat"));
				if (!reader.IsDBNull(reader.GetOrdinal("NhanKhauID"))) entity.NhanKhauID = reader.GetInt64(reader.GetOrdinal("NhanKhauID"));
				if (!reader.IsDBNull(reader.GetOrdinal("CanBoQuanLyID"))) entity.CanBoQuanLyID = reader.GetInt32(reader.GetOrdinal("CanBoQuanLyID"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;
		}
		
		public static bool Find(List<NhanKhau_QuaTrinh> collection, int iD)
        {
            foreach (NhanKhau_QuaTrinh item in collection)
            {
                if (item.ID == iD)
                {
                    return true;
                }
            }

            return false;
        }
		
		public static void UpdateDataSet(DataSet ds)
        {
            string insert = "Insert INTO t_NhanKhau_QuaTrinh VALUES(@NgayCapNhat, @LoaiCapNhat, @NoiDungThayDoi, @NgayChuyenDen, @ThuongTruTruocChuyenDen, @NgayChuyenDi, @NoiChuyenDen, @NgayMat, @NhanKhauID, @CanBoQuanLyID)";
            string update = "UPDATE t_NhanKhau_QuaTrinh SET NgayCapNhat = @NgayCapNhat, LoaiCapNhat = @LoaiCapNhat, NoiDungThayDoi = @NoiDungThayDoi, NgayChuyenDen = @NgayChuyenDen, ThuongTruTruocChuyenDen = @ThuongTruTruocChuyenDen, NgayChuyenDi = @NgayChuyenDi, NoiChuyenDen = @NoiChuyenDen, NgayMat = @NgayMat, NhanKhauID = @NhanKhauID, CanBoQuanLyID = @CanBoQuanLyID WHERE ID = @ID";
            string delete = "DELETE FROM t_NhanKhau_QuaTrinh WHERE ID = @ID";

			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();

            System.Data.Common.DbCommand InsertCommand = db.GetSqlStringCommand(insert);
			db.AddInParameter(InsertCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayCapNhat", SqlDbType.DateTime, "NgayCapNhat", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@LoaiCapNhat", SqlDbType.Int, "LoaiCapNhat", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NoiDungThayDoi", SqlDbType.NVarChar, "NoiDungThayDoi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayChuyenDen", SqlDbType.DateTime, "NgayChuyenDen", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@ThuongTruTruocChuyenDen", SqlDbType.NVarChar, "ThuongTruTruocChuyenDen", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayChuyenDi", SqlDbType.DateTime, "NgayChuyenDi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NoiChuyenDen", SqlDbType.NVarChar, "NoiChuyenDen", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayMat", SqlDbType.DateTime, "NgayMat", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NhanKhauID", SqlDbType.BigInt, "NhanKhauID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@CanBoQuanLyID", SqlDbType.Int, "CanBoQuanLyID", DataRowVersion.Current);

            System.Data.Common.DbCommand UpdateCommand = db.GetSqlStringCommand(update);
			db.AddInParameter(UpdateCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayCapNhat", SqlDbType.DateTime, "NgayCapNhat", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@LoaiCapNhat", SqlDbType.Int, "LoaiCapNhat", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NoiDungThayDoi", SqlDbType.NVarChar, "NoiDungThayDoi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayChuyenDen", SqlDbType.DateTime, "NgayChuyenDen", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@ThuongTruTruocChuyenDen", SqlDbType.NVarChar, "ThuongTruTruocChuyenDen", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayChuyenDi", SqlDbType.DateTime, "NgayChuyenDi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NoiChuyenDen", SqlDbType.NVarChar, "NoiChuyenDen", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayMat", SqlDbType.DateTime, "NgayMat", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NhanKhauID", SqlDbType.BigInt, "NhanKhauID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@CanBoQuanLyID", SqlDbType.Int, "CanBoQuanLyID", DataRowVersion.Current);
			
            System.Data.Common.DbCommand DeleteCommand = db.GetSqlStringCommand(delete);
			db.AddInParameter(DeleteCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);

            db.UpdateDataSet(ds, ds.Tables[0].TableName, InsertCommand, UpdateCommand, DeleteCommand, UpdateBehavior.Standard);
        }
				
		public static void UpdateDataSet(DataSet ds, string tableName)
        {
            string insert = "Insert INTO t_NhanKhau_QuaTrinh VALUES(@NgayCapNhat, @LoaiCapNhat, @NoiDungThayDoi, @NgayChuyenDen, @ThuongTruTruocChuyenDen, @NgayChuyenDi, @NoiChuyenDen, @NgayMat, @NhanKhauID, @CanBoQuanLyID)";
            string update = "UPDATE t_NhanKhau_QuaTrinh SET NgayCapNhat = @NgayCapNhat, LoaiCapNhat = @LoaiCapNhat, NoiDungThayDoi = @NoiDungThayDoi, NgayChuyenDen = @NgayChuyenDen, ThuongTruTruocChuyenDen = @ThuongTruTruocChuyenDen, NgayChuyenDi = @NgayChuyenDi, NoiChuyenDen = @NoiChuyenDen, NgayMat = @NgayMat, NhanKhauID = @NhanKhauID, CanBoQuanLyID = @CanBoQuanLyID WHERE ID = @ID";
            string delete = "DELETE FROM t_NhanKhau_QuaTrinh WHERE ID = @ID";

			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();

            System.Data.Common.DbCommand InsertCommand = db.GetSqlStringCommand(insert);
			db.AddInParameter(InsertCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayCapNhat", SqlDbType.DateTime, "NgayCapNhat", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@LoaiCapNhat", SqlDbType.Int, "LoaiCapNhat", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NoiDungThayDoi", SqlDbType.NVarChar, "NoiDungThayDoi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayChuyenDen", SqlDbType.DateTime, "NgayChuyenDen", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@ThuongTruTruocChuyenDen", SqlDbType.NVarChar, "ThuongTruTruocChuyenDen", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayChuyenDi", SqlDbType.DateTime, "NgayChuyenDi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NoiChuyenDen", SqlDbType.NVarChar, "NoiChuyenDen", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NgayMat", SqlDbType.DateTime, "NgayMat", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@NhanKhauID", SqlDbType.BigInt, "NhanKhauID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@CanBoQuanLyID", SqlDbType.Int, "CanBoQuanLyID", DataRowVersion.Current);

            System.Data.Common.DbCommand UpdateCommand = db.GetSqlStringCommand(update);
			db.AddInParameter(UpdateCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayCapNhat", SqlDbType.DateTime, "NgayCapNhat", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@LoaiCapNhat", SqlDbType.Int, "LoaiCapNhat", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NoiDungThayDoi", SqlDbType.NVarChar, "NoiDungThayDoi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayChuyenDen", SqlDbType.DateTime, "NgayChuyenDen", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@ThuongTruTruocChuyenDen", SqlDbType.NVarChar, "ThuongTruTruocChuyenDen", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayChuyenDi", SqlDbType.DateTime, "NgayChuyenDi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NoiChuyenDen", SqlDbType.NVarChar, "NoiChuyenDen", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NgayMat", SqlDbType.DateTime, "NgayMat", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@NhanKhauID", SqlDbType.BigInt, "NhanKhauID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@CanBoQuanLyID", SqlDbType.Int, "CanBoQuanLyID", DataRowVersion.Current);
			
            System.Data.Common.DbCommand DeleteCommand = db.GetSqlStringCommand(delete);
			db.AddInParameter(DeleteCommand, "@ID", SqlDbType.Int, "ID", DataRowVersion.Current);

            db.UpdateDataSet(ds, tableName, InsertCommand, UpdateCommand, DeleteCommand, UpdateBehavior.Standard);
        }

		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static NhanKhau_QuaTrinh Load(int iD)
		{
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_Load.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            IDataReader reader = db.ExecuteReader(dbCommand);
			List<NhanKhau_QuaTrinh> collection = ConvertToCollection(reader);	
			if (collection.Count > 0)
			{
				return collection[0];
			}
			return null;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static List<NhanKhau_QuaTrinh> SelectCollectionAll()
		{
			IDataReader reader = SelectReaderAll();
			return ConvertToCollection(reader);			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static List<NhanKhau_QuaTrinh> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			return ConvertToCollection(reader);		
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static List<NhanKhau_QuaTrinh> SelectCollectionBy_NhanKhauID(long nhanKhauID)
		{
            IDataReader reader = SelectReaderBy_NhanKhauID(nhanKhauID);
			return ConvertToCollection(reader);	
		}		
		//---------------------------------------------------------------------------------------------
		public static List<NhanKhau_QuaTrinh> SelectCollectionBy_CanBoQuanLyID(int canBoQuanLyID)
		{
            IDataReader reader = SelectReaderBy_CanBoQuanLyID(canBoQuanLyID);
			return ConvertToCollection(reader);	
		}		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectBy_NhanKhauID(long nhanKhauID)
		{
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_SelectBy_NhanKhauID.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, nhanKhauID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------
		public static DataSet SelectBy_CanBoQuanLyID(int canBoQuanLyID)
		{
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_SelectBy_CanBoQuanLyID.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@CanBoQuanLyID", SqlDbType.Int, canBoQuanLyID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_NhanKhau_QuaTrinh_SelectAll.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_NhanKhau_QuaTrinh_SelectDynamic.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_NhanKhau_QuaTrinh_SelectAll.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_NhanKhau_QuaTrinh_SelectDynamic.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IDataReader SelectReaderBy_NhanKhauID(long nhanKhauID)
		{
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_SelectBy_NhanKhauID.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, nhanKhauID);
			
            return db.ExecuteReader(dbCommand);
		}		
		//---------------------------------------------------------------------------------------------
		public static IDataReader SelectReaderBy_CanBoQuanLyID(int canBoQuanLyID)
		{
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_SelectBy_CanBoQuanLyID.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@CanBoQuanLyID", SqlDbType.Int, canBoQuanLyID);
			
            return db.ExecuteReader(dbCommand);
		}		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertNhanKhau_QuaTrinh(DateTime ngayCapNhat, int loaiCapNhat, string noiDungThayDoi, DateTime ngayChuyenDen, string thuongTruTruocChuyenDen, DateTime ngayChuyenDi, string noiChuyenDen, DateTime ngayMat, long nhanKhauID, int canBoQuanLyID)
		{
			NhanKhau_QuaTrinh entity = new NhanKhau_QuaTrinh();	
			entity.NgayCapNhat = ngayCapNhat;
			entity.LoaiCapNhat = loaiCapNhat;
			entity.NoiDungThayDoi = noiDungThayDoi;
			entity.NgayChuyenDen = ngayChuyenDen;
			entity.ThuongTruTruocChuyenDen = thuongTruTruocChuyenDen;
			entity.NgayChuyenDi = ngayChuyenDi;
			entity.NoiChuyenDen = noiChuyenDen;
			entity.NgayMat = ngayMat;
			entity.NhanKhauID = nhanKhauID;
			entity.CanBoQuanLyID = canBoQuanLyID;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_Insert.generated]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
			db.AddInParameter(dbCommand, "@NgayCapNhat", SqlDbType.DateTime, NgayCapNhat.Year <= 1753 ? DBNull.Value : (object) NgayCapNhat);
			db.AddInParameter(dbCommand, "@LoaiCapNhat", SqlDbType.Int, LoaiCapNhat);
			db.AddInParameter(dbCommand, "@NoiDungThayDoi", SqlDbType.NVarChar, NoiDungThayDoi);
			db.AddInParameter(dbCommand, "@NgayChuyenDen", SqlDbType.DateTime, NgayChuyenDen.Year <= 1753 ? DBNull.Value : (object) NgayChuyenDen);
			db.AddInParameter(dbCommand, "@ThuongTruTruocChuyenDen", SqlDbType.NVarChar, ThuongTruTruocChuyenDen);
			db.AddInParameter(dbCommand, "@NgayChuyenDi", SqlDbType.DateTime, NgayChuyenDi.Year <= 1753 ? DBNull.Value : (object) NgayChuyenDi);
			db.AddInParameter(dbCommand, "@NoiChuyenDen", SqlDbType.NVarChar, NoiChuyenDen);
			db.AddInParameter(dbCommand, "@NgayMat", SqlDbType.DateTime, NgayMat.Year <= 1753 ? DBNull.Value : (object) NgayMat);
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, NhanKhauID);
			db.AddInParameter(dbCommand, "@CanBoQuanLyID", SqlDbType.Int, CanBoQuanLyID);
			
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				ID = (int) db.GetParameterValue(dbCommand, "@ID");
				return ID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				ID = (int) db.GetParameterValue(dbCommand, "@ID");
				return ID;
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(List<NhanKhau_QuaTrinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (NhanKhau_QuaTrinh item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateNhanKhau_QuaTrinh(int iD, DateTime ngayCapNhat, int loaiCapNhat, string noiDungThayDoi, DateTime ngayChuyenDen, string thuongTruTruocChuyenDen, DateTime ngayChuyenDi, string noiChuyenDen, DateTime ngayMat, long nhanKhauID, int canBoQuanLyID)
		{
			NhanKhau_QuaTrinh entity = new NhanKhau_QuaTrinh();			
			entity.ID = iD;
			entity.NgayCapNhat = ngayCapNhat;
			entity.LoaiCapNhat = loaiCapNhat;
			entity.NoiDungThayDoi = noiDungThayDoi;
			entity.NgayChuyenDen = ngayChuyenDen;
			entity.ThuongTruTruocChuyenDen = thuongTruTruocChuyenDen;
			entity.NgayChuyenDi = ngayChuyenDi;
			entity.NoiChuyenDen = noiChuyenDen;
			entity.NgayMat = ngayMat;
			entity.NhanKhauID = nhanKhauID;
			entity.CanBoQuanLyID = canBoQuanLyID;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "[dbo].p_NhanKhau_QuaTrinh_InsertUpdate.generated]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			db.AddInParameter(dbCommand, "@NgayCapNhat", SqlDbType.DateTime, NgayCapNhat.Year <= 1753 ? DBNull.Value : (object) NgayCapNhat);
			db.AddInParameter(dbCommand, "@LoaiCapNhat", SqlDbType.Int, LoaiCapNhat);
			db.AddInParameter(dbCommand, "@NoiDungThayDoi", SqlDbType.NVarChar, NoiDungThayDoi);
			db.AddInParameter(dbCommand, "@NgayChuyenDen", SqlDbType.DateTime, NgayChuyenDen.Year <= 1753 ? DBNull.Value : (object) NgayChuyenDen);
			db.AddInParameter(dbCommand, "@ThuongTruTruocChuyenDen", SqlDbType.NVarChar, ThuongTruTruocChuyenDen);
			db.AddInParameter(dbCommand, "@NgayChuyenDi", SqlDbType.DateTime, NgayChuyenDi.Year <= 1753 ? DBNull.Value : (object) NgayChuyenDi);
			db.AddInParameter(dbCommand, "@NoiChuyenDen", SqlDbType.NVarChar, NoiChuyenDen);
			db.AddInParameter(dbCommand, "@NgayMat", SqlDbType.DateTime, NgayMat.Year <= 1753 ? DBNull.Value : (object) NgayMat);
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, NhanKhauID);
			db.AddInParameter(dbCommand, "@CanBoQuanLyID", SqlDbType.Int, CanBoQuanLyID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(List<NhanKhau_QuaTrinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (NhanKhau_QuaTrinh item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateNhanKhau_QuaTrinh(int iD, DateTime ngayCapNhat, int loaiCapNhat, string noiDungThayDoi, DateTime ngayChuyenDen, string thuongTruTruocChuyenDen, DateTime ngayChuyenDi, string noiChuyenDen, DateTime ngayMat, long nhanKhauID, int canBoQuanLyID)
		{
			NhanKhau_QuaTrinh entity = new NhanKhau_QuaTrinh();			
			entity.ID = iD;
			entity.NgayCapNhat = ngayCapNhat;
			entity.LoaiCapNhat = loaiCapNhat;
			entity.NoiDungThayDoi = noiDungThayDoi;
			entity.NgayChuyenDen = ngayChuyenDen;
			entity.ThuongTruTruocChuyenDen = thuongTruTruocChuyenDen;
			entity.NgayChuyenDi = ngayChuyenDi;
			entity.NoiChuyenDen = noiChuyenDen;
			entity.NgayMat = ngayMat;
			entity.NhanKhauID = nhanKhauID;
			entity.CanBoQuanLyID = canBoQuanLyID;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_Update.generated]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			db.AddInParameter(dbCommand, "@NgayCapNhat", SqlDbType.DateTime, NgayCapNhat.Year <= 1753 ? DBNull.Value : (object) NgayCapNhat);
			db.AddInParameter(dbCommand, "@LoaiCapNhat", SqlDbType.Int, LoaiCapNhat);
			db.AddInParameter(dbCommand, "@NoiDungThayDoi", SqlDbType.NVarChar, NoiDungThayDoi);
			db.AddInParameter(dbCommand, "@NgayChuyenDen", SqlDbType.DateTime, NgayChuyenDen.Year <= 1753 ? DBNull.Value : (object) NgayChuyenDen);
			db.AddInParameter(dbCommand, "@ThuongTruTruocChuyenDen", SqlDbType.NVarChar, ThuongTruTruocChuyenDen);
			db.AddInParameter(dbCommand, "@NgayChuyenDi", SqlDbType.DateTime, NgayChuyenDi.Year <= 1753 ? DBNull.Value : (object) NgayChuyenDi);
			db.AddInParameter(dbCommand, "@NoiChuyenDen", SqlDbType.NVarChar, NoiChuyenDen);
			db.AddInParameter(dbCommand, "@NgayMat", SqlDbType.DateTime, NgayMat.Year <= 1753 ? DBNull.Value : (object) NgayMat);
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, NhanKhauID);
			db.AddInParameter(dbCommand, "@CanBoQuanLyID", SqlDbType.Int, CanBoQuanLyID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(List<NhanKhau_QuaTrinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (NhanKhau_QuaTrinh item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteNhanKhau_QuaTrinh(int iD)
		{
			NhanKhau_QuaTrinh entity = new NhanKhau_QuaTrinh();
			entity.ID = iD;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_Delete.generated]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static int DeleteBy_NhanKhauID(long nhanKhauID)
		{
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_DeleteBy_NhanKhauID.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@NhanKhauID", SqlDbType.BigInt, nhanKhauID);
						
            return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
			
		public static int DeleteBy_CanBoQuanLyID(int canBoQuanLyID)
		{
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_DeleteBy_CanBoQuanLyID.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@CanBoQuanLyID", SqlDbType.Int, canBoQuanLyID);
						
            return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
			
		
		public static int DeleteDynamic(string whereCondition)
		{
			const string spName = "[dbo].[p_NhanKhau_QuaTrinh_DeleteDynamic.generated]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            
            return db.ExecuteNonQuery(dbCommand);   
		}
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(List<NhanKhau_QuaTrinh> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (NhanKhau_QuaTrinh item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
		
        //---------------------------------------------------------------------------------------------
		
        #region ICloneable Members

        public object Clone()
        {
            return base.MemberwiseClone();
        }

        public NhanKhau_QuaTrinh CloneObject()
        {
            return this.Clone() as NhanKhau_QuaTrinh;
        }
        
        #endregion
	}	
}